package com.lb.jwitter.action.util;
import java.sql.ResultSet;
import java.util.*;
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.connectDatabase.ConnectMySqlDataBase;
import com.lb.utils.orm.hibernate.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml")
public class LoginAction
{
	
	private String username;
	
	private String password;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	private Message msg;
	
	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}


public String execute() throws Exception
{
   	try{
   		ConnectMySqlDataBase connection=new ConnectMySqlDataBase();
		connection.getConnection();
		Map session = ActionContext.getContext().getSession();
		if(session.get("taiKhoan") != null) 
			 return Action.SUCCESS;

	    session.put("taiKhoan", null);
	    HashMap taiKhoan=new HashMap();
	    
	    
		String query="SELECT * FROM tbl_taikhoan where username= '"+username+"' and password='"+password+"'";
		ResultSet rs=connection.getData(query);
		if(rs.next()){
			taiKhoan.put("username", username);
			taiKhoan.put("password", password);
			session.put("taiKhoan", taiKhoan);
			return Action.SUCCESS;
		}
		else{
			 return "noprivilege";
		}

     }
        catch(Exception e){
            System.out.println("Loi log in");
            e.printStackTrace();
            return Action.ERROR;
      }
       
}	
}
